package cn.nj.springsecurity.Controller.shiroController;

import cn.nj.springsecurity.POJO.shiroentity.SysUserEntity;
import cn.nj.springsecurity.common.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * Package: cn.nj.springsecurity.Controller.shiroController
 *用户登录
 * @Author: zhaotianyu
 * @Date: 2019/12/27
 */
@RestController
@RequestMapping("/userLogin")
public class UserLoginController {


    /**
     * 登录
     * @Author Sans
     * @CreateTime 2019/6/20 9:21
     */
    @GetMapping("/login")
    public Map<String,Object> login(@RequestBody SysUserEntity sysUserEntity){
        Map<String,Object> map = new HashMap<>();
        //进行身份验证
        try {
            //验证身份和登陆
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(sysUserEntity.getUsername(),sysUserEntity.getPassword());
            //验证成功进行登录操作
            subject.login(token);
        }catch (IncorrectCredentialsException e){
            map.put("code",500);
            map.put("msg","用户不存在或者密码错误");
            return map;
        }catch (LockedAccountException e){
            map.put("code",500);
            map.put("msg","登录失败，该用户已被冻结");
            return map;
        }catch (AuthenticationException e) {
            map.put("code",500);
            map.put("msg","该用户不存在");
            return map;
        }catch (Exception e) {
            map.put("code",500);
            map.put("msg","未知异常");
            return map;
        }  map.put("code",0);
        map.put("msg","登录成功");
        map.put("token", ShiroUtils.getSession().getId().toString());
        return map;

    }

    /**
     * 未登录
     */
    @GetMapping("/unauth")
    public Map<String,Object> unauth(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",500);
        map.put("msg","未登录");
        return map;
    }







}
